package com.jz.community.modulemine.myCard.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jz.community.basecomm.base.BaseX5WebActivity;
import com.jz.community.basecomm.bean.baseCardInfo.BaseCardInfo;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.constant.HtmlConstant;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.commview.view.cardstack.RxAdapterStack;
import com.jz.community.commview.view.cardstack.RxCardStackView;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.money.ui.MoneyExchangeActivity;
import com.jz.community.modulemine.money.ui.MoneyRechargeActivity;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Tony.Fan on 2018/8/22 11:43
 */
public class MyCardAdapter extends RxAdapterStack<BaseCardInfo> {

    public interface ToCardDetailResult {

        void cardToResult(String cardInfo);
    }

    private ToCardDetailResult toCardDetailResult;

    public ToCardDetailResult getToCardDetailResult() {
        return toCardDetailResult;
    }

    public void setToCardDetailResult(ToCardDetailResult toCardDetailResult) {
        this.toCardDetailResult = toCardDetailResult;
    }

    public MyCardAdapter(Context context) {
        super(context);
    }

    @Override
    public void bindView(BaseCardInfo item, int position, RxCardStackView.ViewHolder holder) {

        if (holder instanceof CardItemViewHolder) {
            CardItemViewHolder cardItemViewHolder = (CardItemViewHolder) holder;
            cardItemViewHolder.onBind(item);
        }
    }

    @Override
    protected RxCardStackView.ViewHolder onCreateView(ViewGroup parent, int viewType) {
        return new CardItemViewHolder(getLayoutInflater().inflate(R.layout.module_mine_item_card_mine, parent, false));
    }

    class CardItemViewHolder extends RxCardStackView.ViewHolder {

        CircleImageView itemCardMineImg;
        TextView itemCardMineName;
        TextView itemCardMineMoney;
        LinearLayout itemCardMineMoneyLl;
        TextView itemCardMineDate;
        RelativeLayout itemCardMineTopRl;
        LinearLayout itemCardMineHelp;
        LinearLayout itemCardMineUse;
        LinearLayout itemCardMineDetail;
        RelativeLayout itemCardMineBotRl;
        TextView toExchangeTv;
        TextView itemCardMineHelpTv;
        ImageView cardAddMoneyTv;

        public CardItemViewHolder(View view) {
            super(view);
            itemCardMineImg = view.findViewById(R.id.item_card_mine_img);
            itemCardMineName = view.findViewById(R.id.item_card_mine_name);
            itemCardMineMoney = view.findViewById(R.id.item_card_mine_money);
            itemCardMineMoneyLl = view.findViewById(R.id.item_card_mine_money_ll);
            itemCardMineDate = view.findViewById(R.id.item_card_mine_date);
            itemCardMineTopRl = view.findViewById(R.id.item_card_mine_top_rl);
            itemCardMineHelp = view.findViewById(R.id.item_card_mine_help);
            itemCardMineUse = view.findViewById(R.id.item_card_mine_use);
            itemCardMineDetail = view.findViewById(R.id.item_card_mine_detail);
            itemCardMineBotRl = view.findViewById(R.id.item_card_mine_bot_rl);
            toExchangeTv = view.findViewById(R.id.to_exchange_tv);
            itemCardMineHelpTv = view.findViewById(R.id.item_card_mine_help_tv);
            cardAddMoneyTv = view.findViewById(R.id.card_add_money_tv);
        }

        @Override
        public void onItemExpand(boolean b, int position) {

            int bgColor = Color.parseColor(!Preconditions.isNullOrEmpty(getItem(position).getColor()) ? getItem(position).getColor() : "#f2383b");

            if (b) {
                GradientDrawable drawableTop = (GradientDrawable) getContext().getResources().getDrawable(R.drawable.border_card_mine_top);
                drawableTop.setColor(bgColor);

                GradientDrawable drawableBot = (GradientDrawable) getContext().getResources().getDrawable(R.drawable.border_card_mine_bot);
                drawableBot.setColor(bgColor);

                itemCardMineTopRl.setBackground(drawableTop);

                itemCardMineBotRl.setBackground(drawableBot);

                SHelper.vis(itemCardMineBotRl);

            } else {
                GradientDrawable drawableTop = (GradientDrawable) getContext().getResources().getDrawable(R.drawable.border_card_mine_4_rad);
                drawableTop.setColor(bgColor);
                itemCardMineTopRl.setBackground(drawableTop);

                if (getItemCount() - 1 == position) {
                    SHelper.vis(itemCardMineBotRl);
                    onItemExpand(true, position);
                } else {
                    SHelper.gone(itemCardMineBotRl);
                }
            }
        }

        @Override
        public void onAnimationStateChange(int state, boolean willBeSelect, int position) {
            super.onAnimationStateChange(state, willBeSelect, position);
            if (state == RxCardStackView.ANIMATION_STATE_START && willBeSelect) {
                onItemExpand(true, position);
            }
            if (state == RxCardStackView.ANIMATION_STATE_END && !willBeSelect) {
                onItemExpand(false, position);
            }
        }

        public void onBind(BaseCardInfo item) {

            BaseImageLoaderUtils.getInstance().loadDefaltImage(getContext(), itemCardMineImg, item.getBrandIcon());

            itemCardMineName.setText(item.getBrand());

            //社区人E卡 、壳牌
            if (item.getCardtype() == 999 || item.getCardtype() == 2) {
                SHelper.vis(itemCardMineMoneyLl);
                itemCardMineMoney.setText(AppConstants.MONEY + item.getAmount());
            } else {
                SHelper.gone(itemCardMineMoneyLl);
            }

            if (item.getExpireDays() > 0 && item.getExpireDays() < 3) {
                itemCardMineDate.setText(item.getExpireDays() + "天后失效");
            } else {
                SHelper.gone(itemCardMineDate);
            }

            //卡密类型
            if (item.getCardtype() == 0) {
                SHelper.gone(itemCardMineUse);
            } else {
                SHelper.vis(itemCardMineUse);
            }

            if (!Preconditions.isNullOrEmpty(item.getColor())) {
                int bgColor = Color.parseColor(item.getColor());
                itemCardMineTopRl.getBackground().setColorFilter(bgColor, PorterDuff.Mode.SRC_IN);
                itemCardMineBotRl.getBackground().setColorFilter(bgColor, PorterDuff.Mode.SRC_IN);
            }

            if (item.getCardtype() == 999) {
                toExchangeTv.setText("兑换");
                itemCardMineHelpTv.setText("充值");
                cardAddMoneyTv.setImageResource(R.mipmap.card_mine_charge);
            } else {
                itemCardMineHelpTv.setText("使用帮助");
                toExchangeTv.setText("去使用");
                cardAddMoneyTv.setImageResource(R.mipmap.card_mine_help);
            }

            itemCardMineHelp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.getCardtype() == 999) {
                        Intent intent = new Intent(getContext(), MoneyRechargeActivity.class);
                        if (!RxDataTool.isNullString(item.getAmount() + "")) {
                            intent.putExtra("myMoney", item.getAmount() + "");
                        } else {
                            intent.putExtra("myMoney", "0");
                        }
                        getContext().startActivity(intent);
                    } else {
                        getContext().startActivity(new Intent(getContext(), BaseX5WebActivity.class)
                                .putExtra("url", HtmlConstant.CARD_HTML_URL + item.getProductId())
                                .putExtra("title", "帮助"));
                    }
                }
            });

            itemCardMineUse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.getCardtype() == 999) {
                        Intent intent = new Intent(getContext(), MoneyExchangeActivity.class);
                        getContext().startActivity(intent);
                    } else if (item.getCardtype() == 2) {
                        Bundle bundle = new Bundle();
                        bundle.putString("cardno", item.getCardno());
                        bundle.putString("cardId", item.getId());
                        bundle.putInt("cardType", item.getCardtype());
                        RouterCommonUtils.startParamsActivity(RouterIntentConstant.CARD_SCAN, bundle);
                    } else {
                        getContext().startActivity(new Intent(getContext(), BaseX5WebActivity.class)
                                .putExtra("url", item.getCardno())
                                .putExtra("title", item.getBrand()));
                    }
                }
            });

            itemCardMineDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toCardDetailResult.cardToResult(JSON.toJSONString(item));
                }
            });
        }
    }
}
